package com.iot.inter.controller.newinterface.query;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.iot.inter.entity.common.OrderPaymentManager;
import com.iot.inter.entity.newpojo.Constant;
import com.iot.inter.entity.newrequest.querypartner.request.RequestQueryPartner;
import com.iot.inter.entity.newrequest.querypartner.response.ResponseQueryPartner;
import com.iot.inter.interfaceprocessing.newinterface.TurnToQueryPartnerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.*;

/**
 * 去北京做：根据iccid查询企业信息:2.0接口
 *
 * @author lm
 * @date 2019/3/4
 */
@RestController
@Api(description = "查询企业信息")
public class QueryPartnerByDeviceIdController {

    private static Logger logger = Logger.getLogger(QueryPartnerByDeviceIdController.class);

    @CrossOrigin
    @RequestMapping(value = "queryPartnerByDeviceId", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "查询企业信息", response = ResponseQueryPartner.class)
    public String queryPartner(@ApiParam @RequestBody RequestQueryPartner request) {

        String jsonString;
        String requestStr = JSON.toJSONString(request);

        if (requestStr != null) {
            logger.info("【根据ICCID查询PartnerCode】请求：" + requestStr);
            RequestQueryPartner requestPackage = JSONArray.parseObject(requestStr, RequestQueryPartner.class);
            String deviceId = requestPackage.getDeviceId();

            TurnToQueryPartnerService queryPartnerService = new TurnToQueryPartnerService();
            OrderPaymentManager manager = queryPartnerService.queryPartner(deviceId);

            if (manager != null) {

                if (true == Constant.RET_CODE_0000.equalsIgnoreCase(manager.getRetCode())) {

                    ResponseQueryPartner responseOrder = new ResponseQueryPartner();
                    responseOrder.setPartnerCode(manager.getPartnerCode());
                    responseOrder.setRetCode(manager.getRetCode());
                    responseOrder.setResultInfo(manager.getResultInfo());
                    // 返回给调用者的报文
                    jsonString = JSON.toJSONString(responseOrder);
                    logger.info("【根据ICCID查询PartnerCode】成功响应：" + jsonString);
                    return jsonString;


                } else {
                    ResponseQueryPartner responseOrder = new ResponseQueryPartner();
                    responseOrder.setRetCode(manager.getRetCode());
                    responseOrder.setResultInfo(manager.getResultInfo());
                    // 返回给调用者的报文
                    jsonString = JSON.toJSONString(responseOrder);
                    logger.info("【根据ICCID查询PartnerCode】响应：" + jsonString);
                    return jsonString;

                }

            } else {

                ResponseQueryPartner responseOrder = new ResponseQueryPartner();
                responseOrder.setRetCode("9999");
                responseOrder.setResultInfo("接口返回未知错误");
                // 返回给调用者的报文
                jsonString = JSON.toJSONString(responseOrder);
                logger.info("【根据ICCID查询PartnerCode】响应：" + jsonString);
                return jsonString;

            }


        } else {
            ResponseQueryPartner responseOrder = new ResponseQueryPartner();
            responseOrder.setRetCode("9998");
            responseOrder.setResultInfo("请求为空!");
            // 返回给调用者的报文
            jsonString = JSON.toJSONString(responseOrder);
            logger.info("【根据ICCID查询PartnerCode】响应：" + jsonString);
            return jsonString;
        }

    }

}
